#include "widget.h"
#include "ui_widget.h"
#include "QMessageBox"

#define  D  6
#define  IP "192.168.1.207"
//#define  IP "192.168.1.12"
#define  Port 6666
msg revmsg;
msg sendmsg;
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    memset(&revmsg,0,sizeof(msg));
    memset(&sendmsg,0,sizeof(msg));
    ui->stackedWidget->setCurrentIndex(0);
    setWindowTitle("登录窗口");
    cli_soc = new QTcpSocket (this);
    //camsoc = new QTcpSocket (this);
    ui->history_tableWidget->setRowCount(20);
    ui->history_tableWidget->setColumnCount(1);
    QStringList header ;
    header<<"历史记录";
    ui->history_tableWidget->setHorizontalHeaderLabels(header);
    ui->tabWidget->setCurrentIndex(0);
    ui->history_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    connect(cli_soc, SIGNAL(connected()),this,SLOT(connctedSlot()));
   // connect(camsoc, SIGNAL(connected()),this,SLOT(connctedSlot()));
   // camsoc->connectToHost(QHostAddress(IP),4567);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::connctedSlot2()
{
    qDebug()<<"link22 ok";
 connect(camsoc, SIGNAL(readyRead()), this, SLOT(recvImgs()));
}
void Widget::connctedSlot()
{
    qDebug()<<"link ok";
 connect(cli_soc, SIGNAL(readyRead()), this, SLOT(slotRecvData()));
}

int i=0;
void Widget::slotRecvData()
{
    memset(&revmsg,0,sizeof(msg));
    cli_soc->read((char*)&revmsg,sizeof(msg));
    QByteArray arr =revmsg.name;
    arr.append(revmsg.data);
    qDebug()<<revmsg.type;
    qDebug()<<revmsg.data;
    if(revmsg.type==TH)
    {
        qDebug()<<"************";
        QString buf(revmsg.data);
        QStringList strlist;
        strlist = buf.split(",");
        ui->tempLabel->setText(strlist.at(0));
        ui->humLabel->setText(strlist.at(1));
    }
    else if(revmsg.type==history)
    {
        QString tem(revmsg.data);
        ui->history_tableWidget->setItem(i,0,new QTableWidgetItem(tem));
        i++;
    }
    else
    {
    ui->textBrowser->setText(QString(arr)); 
    QString point;
    point=revmsg.data;

    if("loginOk" == point)
    {
        QMessageBox::information(this, "登录", "登录成功!");
        ui->stackedWidget->setCurrentIndex(1); //隐藏对话框,切换到主界面
        setWindowTitle("主窗口");
    }
    else if("loginPswError" == point)
    {
        QMessageBox::information(this, "登录", "密码错误,请重新输入!");
        ui->loginname_line_edit ->clear();
        ui->login_pass_line_edit ->clear();
    }
    else if("loginNoRegister" == point)
    {
        QMessageBox::information(this, "登录", "未注册，请先注册!");
        ui->loginname_line_edit ->clear();
        ui->login_pass_line_edit ->clear();
    }
    else if("registerOk" == point)
    {
        QMessageBox::information(this, "注册", "注册成功!");
        ui->register_name_lineEdit->clear();
        ui->register_pass2_lineEdit->clear();
        ui->register_pass_lineEdit->clear();
    }
    else if("registerError" == point)
    {
        QMessageBox::information(this, "注册", "已经注册，请登录!");
        ui->register_name_lineEdit->clear();
        ui->register_pass2_lineEdit->clear();
        ui->register_pass_lineEdit->clear();
    }
    }
}


void Widget::on_addcard_btn_clicked()
{
    memset(&sendmsg,0,sizeof(msg));
    QString str1=ui->name_line_edit->text();
    QString str2=ui->cardno_line_edit->text();
    if(str1.isEmpty()||str2.isEmpty())
    {
        QMessageBox::information(this,"提示","用户名或密码不能为空");
        return;
    }
    QByteArray tem1 = str1.toLatin1();
    QByteArray tem2 = str2.toLatin1();
    sendmsg.type=authorization;
    memcpy(sendmsg.name,tem1.data(),64);
    memcpy(sendmsg.data,tem2.data(),512);
    cli_soc->write((char*)&sendmsg,sizeof(msg));
}


void Widget::on_delete_card_btn_clicked()
{
    memset(&sendmsg,0,sizeof(msg));
    QString str1=ui->delcard_line_edit->text();
    QString str2 = "card";
    if(str1.isEmpty()||str2.isEmpty())
    {
        QMessageBox::information(this,"提示","用户名或密码不能为空");
        return;
    }
    QByteArray tem1 = str1.toLatin1();
    QByteArray tem2 = str2.toLatin1();
    sendmsg.type=D;
    memcpy(sendmsg.name,tem2.data(),64);
    memcpy(sendmsg.data,tem1.data(),512);
    cli_soc->write((char *)&sendmsg,sizeof(msg));
}

void Widget::on_pushButton_6_clicked()
{
    memset(&sendmsg,0,sizeof(msg));
    QString str1=ui->delusername_line_edit->text();
    QString str2=ui->deluserpassword_line_edit->text();
    if(str1.isEmpty()||str2.isEmpty())
    {
        QMessageBox::information(this,"提示","用户名或密码不能为空");
        return;
    }
    QByteArray tem1 = str1.toLatin1();
    QByteArray tem2 = str2.toLatin1();
    sendmsg.type=D;
    memcpy(sendmsg.name,tem1.data(),64);
    memcpy(sendmsg.data,tem2.data(),512);
    cli_soc->write((char*)&sendmsg,sizeof(msg));

}

void Widget::on_login_btn_clicked()
{
    if(QAbstractSocket::UnconnectedState ==cli_soc->state())  //若已经连接服务器，直接登录
    {
        cli_soc->connectToHost(QHostAddress(IP),Port);
        this->checkAccessSlot();
    }
    else
    {
        this->checkAccessSlot();
    }
}
void  Widget::checkAccessSlot()      //向服务器发送用户登录信息，用户名和密码
{
    memset(&sendmsg,0,sizeof(msg));
    QString str1=ui->loginname_line_edit->text();
    QString str2=ui->login_pass_line_edit ->text();
    if(str1.isEmpty()||str2.isEmpty())
    {
        QMessageBox::information(this,"提示","用户名或密码不能为空");
        return;
    }
    QByteArray tem1 = str1.toLatin1();
    QByteArray tem2 = str2.toLatin1();
    sendmsg.type=login;
    memcpy(sendmsg.name,tem1.data(),64);
    memcpy(sendmsg.data,tem2.data(),512);
    cli_soc->write((char*)&sendmsg,sizeof(msg));
}
void Widget::on_register_btn_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    setWindowTitle("注册窗口");
    if(QAbstractSocket::UnconnectedState ==cli_soc->state())
    {
        cli_soc->connectToHost(QHostAddress(IP),Port);
    }

}

void Widget::on_querenregister_btn_clicked()
{
    if(ui->register_pass_lineEdit->text() != ui->register_pass2_lineEdit->text())
    {
        QMessageBox::warning(this,"警告","密码不一致，请重新输入", QMessageBox::Yes,QMessageBox::Yes);
        ui->register_pass_lineEdit->clear();
        ui->register_pass2_lineEdit->clear();
    }
    else if(ui->register_name_lineEdit->text().isEmpty() || ui->register_pass_lineEdit->text().isEmpty() || ui->register_pass2_lineEdit->text().isEmpty())
    {
        QMessageBox::warning(this,"警告","输入不能为空，请重新输入", QMessageBox::Yes,QMessageBox::Yes);
        ui->register_name_lineEdit->clear();
        ui->register_pass_lineEdit->clear();
        ui->register_pass2_lineEdit->clear();
    }
    else
    {
        memset(&sendmsg,0,sizeof(msg));
        QString str1=ui->register_name_lineEdit->text();
        QString str2=ui->register_pass_lineEdit->text();
        QByteArray tem1 = str1.toLatin1();
        QByteArray tem2 = str2.toLatin1();
        sendmsg.type=registered;
        memcpy(sendmsg.name,tem1.data(),64);
        memcpy(sendmsg.data,tem2.data(),512);
        cli_soc->write((char*)&sendmsg,sizeof(msg));
    }

}

void Widget::on_fanhuilogin_btn_clicked()
{
     ui->stackedWidget->setCurrentIndex(0);
     setWindowTitle("登录窗口");
}

void Widget::on_historyfanhui_btn_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    setWindowTitle("主窗口");
}

void Widget::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
    setWindowTitle("历史记录窗口");
}

void Widget::on_his_queding_btn_clicked()
{
    if(QAbstractSocket::UnconnectedState ==cli_soc->state())  //若已经连接服务器，直接登录
    {
        cli_soc->connectToHost(QHostAddress(IP),Port);
    }
    i=0;
    memset(&sendmsg,0,sizeof(msg));
    QString str1=ui->time_start_lineedit->text()+"*"+ui->time_end_lineEdit->text();
    //QByteArray tem1 = str1.toLatin1();
    QByteArray tem = str1.toLatin1();
    sendmsg.type=history;
    strcpy(sendmsg.data,tem.data());
    cli_soc->write((char*)&sendmsg,sizeof(msg));

}



void Widget::on_set_ok_btn_clicked()
{
        memset(&sendmsg,0,sizeof(msg));
        QString str3=ui->set_Hum_up->text();
        QString str4=ui->set_Hum_down->text();
        QString str1=ui->set_Temp_up->text();
        QString str2=ui->set_Temp_down->text();
        if(str1.isEmpty()||str2.isEmpty()||str3.isEmpty()||str4.isEmpty())
        {
            QMessageBox::information(this,"提示","设置阈值不能为空");
            return;
        }
        sendmsg.type= Set;
        QString buf = str1+","+str2+","+str3+","+str4;
        memcpy(sendmsg.data, buf.toLatin1().data() ,512);
        cli_soc->write((char*)&sendmsg,sizeof(msg));
        ui->tem_up_label->setText(str1);
        ui->tem_down_label->setText(str2);
        ui->hum_up_label->setText(str3);
        ui->hum_down_label->setText(str4);

}
/******************************/
/*void Widget::recvImgs()
{
    qDebug() <<"9999";
    QByteArray by = camsoc->readAll();
    QString arr(by);
    QStringList strlist;
    strlist = arr.split("*");
    imageData.append(strlist.at(1));
     QString arr2(strlist.at(0));
     qDebug() << "++++++"<<strlist.at(0);
    if(imageData.size()==arr2.toInt())
    {
        qDebug() <<"*********";
        showpicture();
        imageData.clear();
    }
    else
    {
        return;
    }



}*/
void Widget::showpicture()
{
    qDebug() << "0000";
    QPixmap pixmap ;
    QImage image= QImage::fromData(imageData,"jpg");
    pixmap = pixmap.fromImage(image);

    ui->video_Label->setPixmap(pixmap);
    ui->video_Label->setAlignment(Qt::AlignCenter);
    ui->video_Label->show();
}
void Widget::on_pushButton_4_clicked()
{
    /*if(QAbstractSocket::UnconnectedState ==cli_soc->state())  //若已经连接服务器，直接登录
    {
        camsoc->connectToHost(QHostAddress(IP),4567);
    }*/
}
